package com.lanou.demo.backgrounddemo.controller;

import com.lanou.entity.utils.UploadPictureUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 常宏盛
 * Date: 2019/7/18 8:50
 **/
@RestController
public class UploadPictureController {

    @Value("${accessKey}")
    public String accessKey;

    @Value("${secretKey}")
    public String secretKey;

    @Value("${bucket}")
    public String bucket;

    @Value("${domain}")
    public String domain;

    @PostMapping("/pic/upload")
    public Map<String, Object> uploadPicture(MultipartFile uploadFile) {
        Map<String, Object> map = new HashMap<>();
        String message = UploadPictureUtils.uploadPicture(uploadFile, this.accessKey, this.secretKey, this.bucket, this.domain);
        if ("error".equals(message)) {
            map.put("error", 1);
            map.put("message", "上传出错");
            return map;
        }
        map.put("error", 0);
        map.put("url", message);
        return map;
    }

}
